﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Configuration;


public partial class Login : System.Web.UI.Page
{
     private static string connstr = System.Configuration.ConfigurationManager.AppSettings["strConn"].ToString().Trim();//定义连接字符串
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (drpIdentity.SelectedValue == "用户")
        {
            Class1 cc = new Class1();
            SqlConnection conn = cc.Getcon();
            string id = txtID.Text.ToString();
            string ps = txtPS.Text.ToString();
            string ri = drpIdentity.Text.Trim();
            string sql = "select count(*) from user1 where U_id='" + id + "'and U_password='" + ps + "'and U_identity ='" + ri + "'";
            try
            {
                conn.Open();

                SqlCommand cmd = new SqlCommand(sql, conn);

                int result = (int)cmd.ExecuteScalar();//ExecuteScalar()此方法一般用在带有聚合函数的
                if (id == "" || ps == "")
                {
                    Response.Write("<Script>alert('请完整输入姓名和密码！')</script>");
                }
                else
                {
                    int count = cc.Execselect(sql);
                    if (count > 0)
                    {
                        Response.Redirect("~\\chakanxinxi.aspx");
                    }
                    else
                    {
                        Response.Write("<script>alert('姓名和密码错误！');location='Login.aspx'</script>");
                    }


                }
            }
            catch (SqlException ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                conn.Close();//关闭数据库
            }

        }

        else if (drpIdentity.SelectedValue == "管理员")
        {
            {
                Class1 cc = new Class1();
                SqlConnection conn = cc.Getcon();
                string id = txtID.Text.ToString();
                string ps = txtPS.Text.ToString();
                string ri = drpIdentity.Text.Trim();
                string sql = "select count(*) from user1 where U_id='" + id + "'and U_password='" + ps + "'and U_identity ='" + ri + "'";
                try
                {
                    conn.Open();

                    SqlCommand cmd = new SqlCommand(sql, conn);

                    int result = (int)cmd.ExecuteScalar();//ExecuteScalar()此方法一般用在带有聚合函数的
                    if (id == "" || ps == "")
                    {
                        Response.Write("<Script>alert('请完整输入姓名和密码！')</script>");
                    }
                    else
                    {
                        int count = cc.Execselect(sql);
                        if (count > 0)
                        {
                            Response.Redirect("~\\xinxi.aspx");
                        }
                        else
                        {
                            Response.Write("<script>alert('姓名和密码错误！');location='Login.aspx'</script>");
                        }


                    }
                }
                catch (SqlException ex)
                {
                    throw new Exception(ex.Message);
                }
                finally
                {
                    conn.Close();//关闭数据库
                }
            }
        }
    }
        
         protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Redirect("register.aspx");
    }
}